package com.randev.snap;

import java.util.Locale;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;

public class Initialize extends Activity implements OnItemClickListener, OnClickListener, OnCheckedChangeListener {

	ListView lViewLanguageList;
	TextView tViewTitle;
	Button btnProceed;
	CheckBox cBoxDeviceLanguage;
	private final String TAG = "Initialize";
	private String localeCode = "";
	private boolean useDeviceLanguage = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.initialize);
		lViewLanguageList = (ListView) findViewById(R.id.lvLanguages);
		tViewTitle = (TextView) findViewById(R.id.tvLanguageTitle);
		btnProceed = (Button) findViewById(R.id.bProceed);
		cBoxDeviceLanguage = (CheckBox) findViewById(R.id.cbDeviceLanguage);
		
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        boolean useDeviceLanguage = prefs.getBoolean("DeviceLanguage", false);
		setListAdapter();
		lViewLanguageList.setOnItemClickListener(this);
		btnProceed.setOnClickListener(this);
		cBoxDeviceLanguage.setOnCheckedChangeListener(this);
		lViewLanguageList.setSelection(0);
		cBoxDeviceLanguage.setChecked(useDeviceLanguage);
	}

	private void setListAdapter() {
		String[] languagesArray = getResources().getStringArray(R.array.languages_array);
		ArrayAdapter<String> languageListAdapter = new ArrayAdapter<String>(this,
				  android.R.layout.simple_list_item_1, android.R.id.text1, languagesArray);
		lViewLanguageList.setAdapter(languageListAdapter); 
	}

 
	@Override
	public void onClick(View v) {
		int buttonId = v.getId();
		
		switch (buttonId) {
		case R.id.bProceed:
			try {
				Editor editor = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
				editor.putBoolean("FirstTime", false);
				editor.putString("Language", localeCode);
				editor.putBoolean("DeviceLanguage", useDeviceLanguage);
				editor.commit();
				
				Intent loginActivityIntent = new Intent(Initialize.this, LoginActivity.class);
				finish();
				startActivity(loginActivityIntent);
			} catch (ActivityNotFoundException e) {
				Log.w(TAG, "Unable to start login activity: " + e.toString());
			}
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
		String languageCode = "";

		switch (position) {
		case 0:
			languageCode = "en";
			break;
		case 1:
			languageCode = "fr";
			break;
		case 2:
			languageCode = "it";
			break;
		case 3:
			languageCode = "nn";
			break;
		case 4:
			languageCode = "ru";
			break;
		default:
			languageCode = "en";
				
		}
		
		Log.w(TAG, "Selected locale:" + position);
		
		if (!(languageCode.isEmpty() && languageCode.contentEquals(""))) {
			localeCode = languageCode;
			Locale locale = new Locale(languageCode);
			Locale.setDefault(locale);
			Configuration config = new Configuration();
			config.locale = locale;
			getBaseContext().getResources().updateConfiguration(config,
			      getBaseContext().getResources().getDisplayMetrics());
		}
		
		Editor editor = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
		editor.putBoolean("FirstTime", true);
		editor.putString("Language", localeCode);
		useDeviceLanguage = cBoxDeviceLanguage.isChecked();
		editor.putBoolean("DeviceLanguage", useDeviceLanguage);
		editor.commit();
		
		Intent snapMainIntent = new Intent(Initialize.this, SnapMain.class);
		finish();
		startActivity(snapMainIntent);
		
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		int checkBoxId = buttonView.getId();
		Log.w(TAG, "Use device language: (out of switch) " + isChecked);
		switch (checkBoxId) {
		case R.id.cbDeviceLanguage:
			Log.w(TAG, "Use device language: " + isChecked);
			useDeviceLanguage = isChecked;
			
			break;
		}
	}
}
